<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'choujiang';
//exi('xxxx','error');

$return=array();
//查询当前粉丝是否关注当前公众号
$fans = cfc('core')->get_fans($_SESSION['openid']);
$fansinfo = pdo_get('core_fans',array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
$follow = $fansinfo['follow'];
if($_RQ['op']=='choujiang'){
	if($_RQ['rid']){
		$id=intval($_RQ['rid']);
	}
	if(!$id){
		exi('参数错误！','error');
	}
	 //查询对应活动的参数
	$item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//获取当前时间
	$time = time();	
	 //活动奖项数据 转成数组格式
	 $award = json_decode($item['award'],true);
	 //活动奖项数量
	 $awardNum = count($award);
    //当前玩家信息
	$play = pdo_get('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid']));
	//当前玩家得分
	$return['award'] = array('action'=>'award','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['notice'] = array('action'=>'notice','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['info'] = array('action'=>'info','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$item['starttime2']=date('Y-m-d H:m:s',$item['starttime']);
	$item['endtime2']=date('Y-m-d H:m:s',$item['endtime']);
	//判断活动名额和状态
	$wait_id=$this->model()->is_shopgame($id);
	$jsondata=$this->model()->get_checkgame($item,$wait_id);
	$return['errorCode'] = $jsondata['errorCode'];
	$errorCode = $jsondata['errorCode'];
	$return['errorMsg']=$jsondata['errorMsg'];
	if($item['gameTypeSetting'] == 1){
		if($errorCode==0){
		   $luckkey = $this->get_rand($award);
			if ($luckkey != 100) {
					   //查询剩余总金额
					   $custmoney = pdo_getsum('jy_h5game_award',array('uniacid' => $_SESSION['uniacid'],'rid'=>$id,'awardLevel'=>$luckkey),'awardhb');	
					   $lastmoney = $award[$luckkey]['hbSum'] - $custmoney;
					    //exi($custmoney['SUM(awardhb)'],'error');
						//单次中得红包金额
						$fee = rand($award['0']['hbLow']*100,$award['0']['hbHigh']*100)/100;
					   //exi($fee,'error');
					   if($lastmoney<$fee)
					   {
						   $luckkey = 'p8';
						   $errorCode = 1;   
					   }else{
							$errorCode = 0;
							$awarddata = array(
											'rid' => $id,
											'fansID' => $play['fansID'],
											'award' => $award['0']['prizeName'],
											'awardinfo'=>$play['name'],
											'uniacid'=>$_SESSION['uniacid'],
											'awardLevel' => $luckkey,
											'createtime' => time(),
											'ausername'=>$play['ausername'],
											'address'=>$play['address'],
											'aphone'=>$play['tel'],
											'awardhb'=>$fee,
											'awardTime'=>time()
							);
							//中奖后，存入数据
							$this->insert('award',$awarddata);			   
						}
			}else{
				$luckkey = 'p8';
				$errorCode = 1;
			}
			//记录抽奖次数
			$totalnum = pdo_getcolumn('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'totalnum');			
			$totalnum = $totalnum+1;
			pdo_update('jy_h5game_fans',array('totalnum'=>$totalnum),array('fansID' => $play['fansID'],'rid'=>$id,'uniacid'=>$_SESSION['uniacid']));		 			
		}
		//查询玩家中奖次数
		//剩余抽奖次数
		$item['istotalnum']=$totalnum;
		$leftPlayCount=$this->model()->get_lasttimes($item,$wait_id);
		  //用户可提现金额
		$sql = "SELECT SUM(awardhb) FROM " . tablename('jy_h5game_award') . " WHERE uniacid=:uniacid AND rid=:rid AND fansID=:fansID AND awardstatus=0";
        $money = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":rid" => $id,":fansID" => $_SESSION['uid']));
		$money = $money['SUM(awardhb)'];
	}	
	$return['result'] = $luckkey;
	$return['code'] = $errorCode;
	$return['item']=$item;
	$return['play']=$play;
	$return['fansinfo']=$fansinfo;
	$return['awarddata']=$awarddata;
	$return['success']=$errorCode;
	$return['awardlist']=$award;
	$return['fee']=$fee;
	$return['leftPlayCount']=$leftPlayCount;
	$return['money']=$money;
	exi($return);
}
if($_RQ['op']=='sendhb'){
		if($_RQ['rid']){
		          $id=intval($_RQ['rid']);
	    }
	    if(!$id){
		          exi('参数错误！','error');
	    }
	    $item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['redtype']==1){
			$setting = uni_setting();
			$params=array(
				'money'=>$_RQ['fee'],
				'remark'=>$item['title'],
			);
			$res = model('core/member')->payred_out($_SESSION['uid'],$params);
			if($res['type'] == 'error'){
				$res['result_code']='FAIL';
			}
			if($res['result_code']=='SUCCESS'){
				$success = 1;
			}
		}else{
			$params =array(
				'money'=>$_RQ['fee'],
				'mid'=>$_SESSION['uid'],
				'total_num'=>1,
				'remark'=>'活动-'.$item['title'],
			);	 
			$get_money=cfc('red_pack')->get_money($params,1);
			if($get_money){
				$success = 1;
			}
			$res=array();
		}
		$return =array();
		$return['success'] =$success;
		$return['res'] =$res;
		exi($return);
}
if($_RQ['op']=='tixian'){
		if($_RQ['rid']){
		          $id=intval($_RQ['rid']);
	    }
	    if(!$id){
		
		          exi('参数错误！','error');
	    }
	    $item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//用户可提现金额
		$sql = "SELECT SUM(awardhb) FROM " . tablename('jy_h5game_award') . " WHERE uniacid=:uniacid AND rid=:rid AND fansID=:fansID AND awardstatus=0";
                    $money = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":rid" => $id,":fansID" => $_SESSION['uid']));
		             $money = $money['SUM(awardhb)'];
                     if(1<=$money&&$money<=200){					 
						       $setting = uni_setting();
							   $arr['openid'] = $_SESSION['openid'];
		                       $arr['hbname'] = $item['title'];
		                       $arr['body'] = $item['title']."红包";
		                       $arr['fee'] = $money;
		                       $arr['mchid'] = $setting['pay']['wechat_mchid'];
		                       $arr['wxappid'] = $setting['pay']['wechat_appid'];
		          
		                      $res = $this->sendhongbaoto($arr);
							// exi($res,'error');
							//发送成功
							  if($res['result_code'] == 'SUCCESS'){
							   $success = 1;
							   pdo_update('jy_h5game_award',array('awardstatus'=>'1'),array('fansID' => $_SESSION['uid'],'rid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		                      }
							  //微信发送红包出现错误（余额不足/配置参数错误）
							  else{
								  
								 $success = 0; 
							  }
							$return =array();
							$return['success'] =$success;
							$return['res'] =$res;
							
					 }
					 elseif($money<1){
						    $return['success'] = 2;
						    $return['Msg'] ='微信提现最小金额1元';	 
					 }      
					        $return['money'] =$money;
							exi($return);
		
}

